package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.AgentAuth;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.provider.api.model.vo.RoleVo;
import com.weixing.mall.provider.api.service.SysUserRpcService;
import com.weixing.mall.provider.model.query.AgentCommissionQuery;
import com.weixing.mall.provider.model.vo.AgentCommissionVo;
import com.weixing.mall.provider.service.IAgentCommissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 代理商佣金接口
 *
 * @author  gjq
 * @date  2019/10/16
 * @since  1.0
 **/
@RestController
@RequestMapping("agent/commission")
public class AgentCommissionController extends BaseController {
    @Autowired
    private SysUserRpcService sysUserRpcService;
    @Autowired
    private IAgentCommissionService agentCommissionService;

    /**
     * 加载代理商佣金列表--客服用
     *
     * @param query   查询参数
     * @return        佣金列表
     */
    @GetMapping("")
    public Result< IPage<AgentCommissionVo>> list( AgentCommissionQuery query){

        Long uid= AuthUtil.getSysExt().getUid();
        IPage<AgentCommissionVo> pageInfo=null;
        List<RoleVo> list= sysUserRpcService.getRoleById(uid).getData();
        for(RoleVo vo:list){
            if(vo.getRoleCode().equals(GlobalConstant.RoleCode.ADMIN)){
                pageInfo = agentCommissionService.selectListOfPage(initMpPage(),query);
                return ResultUtil.success(pageInfo);
            }
        }
        for(RoleVo vo:list){
            if(vo.getRoleCode().equals(GlobalConstant.RoleCode.KFADMIN)){
                pageInfo = agentCommissionService.selectListOfPage(initMpPage(),query);
                return ResultUtil.success(pageInfo);
            }
        }
        query.setCustomerId(uid.toString());
       pageInfo = agentCommissionService.selectListOfPage(initMpPage(),query);
        return ResultUtil.success(pageInfo);
    }

    /**
     * 查看代理商佣金详情
     * @param id   佣金id
     * @return     详情
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查看代理商佣金")
    public Result<AgentCommissionVo> agentCommission(@PathVariable long id){
        AgentCommissionVo agentCommissionVo= agentCommissionService.commisionDetail(id);
        return ResultUtil.success(agentCommissionVo);
    }

    /**
     * 代理商佣金列表--代理用
     *
     * @param query  查询条件
     * @return       佣金列表
     */
    @GetMapping("/agent")
    public Result<IPage<AgentCommissionVo>> listForAgent(AgentCommissionQuery query){
        AgentAuth auth = AuthUtil.getAgentExt();
        query.setAgentId(auth.getAid());
        IPage<AgentCommissionVo> pageInfo = agentCommissionService.selectListOfPage(initMpPage(),query);
        return ResultUtil.success(pageInfo);
    }

}
